package model.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Time;

import model.common.Common;

public class CommentManagerDAO {
	
	/**
	 * Thêm nội dung thảo luận
	 * @param userName
	 * @param topic
	 * @param time
	 * @param content
	 * @return
	 */
	public boolean AddComment(String userName, String topic, Time time, String content){
		try {
			Common.connect();
			String sql = "INSERT INTO tblComments VALUES(?, ?, ?, ?)";
			
			int dem = 0;
			PreparedStatement pst = Common.conn.prepareStatement(sql);
			
			pst.setString(1, userName);
			pst.setString(2, topic);
			pst.setTime(3, time);
			pst.setString(4, content);
			dem = pst.executeUpdate();
			
			if (dem == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			System.err.println("Da co loi xay ra. Them binh luan khong thanh cong!!!");
			e.printStackTrace();
		} finally {
			Common.disconnect();
		}
		
		return false;
	}

	/**
	 * Xóa phần bình luận của thành viên
	 * @param userName
	 * @param time
	 * @return
	 */
	public boolean DeleteComment(String userName, Time time){
		try {
			Common.connect();
			String sql = "DELETE FROM tblComments WHERE userID = ? AND time = ?";
			
			int dem = 0;
			PreparedStatement pst = Common.conn.prepareStatement(sql);
			
			pst.setString(1, userName);
			pst.setTime(2, time);
			dem = pst.executeUpdate();
			
			if (dem == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			System.err.println("Da co loi xay ra. Khong xoa duoc du lieu!");
			e.printStackTrace();
		} finally {
			Common.disconnect();
		}
		
		return false;
	}
}
